package cn.hl.order.service.impl;

import cn.hl.basic.service.impl.BaseServiceImpl;
import cn.hl.basic.util.LoginContext;
import cn.hl.basic.util.PageList;
import cn.hl.order.domain.OrderPetAcquisition;
import cn.hl.order.dto.OrderDto;
import cn.hl.order.mapper.OrderPetAcquisitionMapper;
import cn.hl.order.query.OrderPetAcquisitionQuery;
import cn.hl.order.service.IOrderPetAcquisitionService;
import cn.hl.org.domain.Employee;
import cn.hl.org.domain.Shop;
import cn.hl.org.mapper.EmployeeMapper;
import cn.hl.org.mapper.ShopMapper;
import cn.hl.pet.domain.Pet;
import cn.hl.pet.domain.SearchMasterMsg;
import cn.hl.pet.mapper.PetMapper;
import cn.hl.pet.mapper.SearchMasterMsgMapper;
import cn.hl.user.domain.Logininfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

/**
 * 业务实现类：
 */
@Slf4j
@Service
@RocketMQMessageListener(topic = "order_topic", consumerGroup = "my-order-tag")
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService, RocketMQListener<OrderPetAcquisition> {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Override
    @Transactional
    public PageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.findByLogininfoId(logininfo.getId());
        // 1.判断shopId为空，就是平台管理员，不为空，就是店铺管理员
        if (employee.getShop_id() != null) {
            Shop shop = shopMapper.findById(employee.getShop_id());
            // 2.判断店铺管理员id和员工id不相等，就是店员，相等，当前登录人是店铺管理员
            if (!shop.getAdminId().equals(employee.getId())) {
                // 3.店员
                query.setEmpId(employee.getId());
            }
            // 4.店铺管理员
            query.setShopId(shop.getId());
        }
        // 5.平台管理员
        return super.queryPage(query);
    }

    @Override
    @Transactional
    public void confirm(OrderDto orderDto) {
        // 1.修改订单状态、金额、支付方式
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.findById(orderDto.getId());
        orderPetAcquisition.setState(1);
        orderPetAcquisition.setPrice(orderDto.getMoney());
        orderPetAcquisition.setPaytype(orderDto.getPayType());
        orderPetAcquisitionMapper.update(orderPetAcquisition);
        // 2.修改寻主消息状态
        Long msgId = orderPetAcquisition.getSearchMasterMsgId();
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.findById(msgId);
        searchMasterMsg.setState(2);
        searchMasterMsgMapper.update(searchMasterMsg);
        // 3.修改宠物金额
        Long petId = orderPetAcquisition.getPetId();
        Pet pet = petMapper.findById(petId);
        pet.setCostprice(orderDto.getMoney());
        petMapper.update(pet);
    }

    @Override
    @Transactional
    public void cancel(Long id) {
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.findById(id);
        // 1.放到寻主池，shopId为空
        searchMasterMsgMapper.reject(orderPetAcquisition.getSearchMasterMsgId());
        // 2.删除宠物信息
        petMapper.delete(orderPetAcquisition.getPetId());
        // 3.取消订单
        orderPetAcquisition.setState(-1);
        orderPetAcquisitionMapper.update(orderPetAcquisition);
    }

    @Override
    public void onMessage(OrderPetAcquisition orderPetAcquisition) {
        OrderPetAcquisition order = orderPetAcquisitionMapper.findById(orderPetAcquisition.getId());
        log.info("收到延迟消息：" + order);
        if (order.getState() == 0) {
            // 1.放到寻主池，shopId为空
            searchMasterMsgMapper.reject(order.getSearchMasterMsgId());
            // 2.删除宠物信息
            petMapper.delete(order.getPetId());
            // 3.取消订单
            order.setState(-1);
            orderPetAcquisitionMapper.update(order);
        }
    }
}
